<template>
    <div>
        <h1>教室管理页面</h1>
        <el-button type="text" @click="openAddClassroom()">新增教室</el-button>
        <el-dialog title="新增教室" :visible.sync="dialogFormVisible">
            <el-form :model="form" :rules="rules" ref="form" label-width="80px">
                <el-form-item label="教室类型">
                    <el-select v-model="form.classroomTypeId" placeholder="请选择教室类型">
                        <el-option v-for="i in type" :label="i.typeName" :value="i.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="教室名" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="教室容量" :label-width="formLabelWidth" prop="capacity">
                    <el-input v-model="form.capacity" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
                    <el-input v-model="form.sort" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="正常使用">
                    <el-switch v-model="form.normal"></el-switch>
                </el-form-item>
                <el-form-item label="教室说明" :label-width="formLabelWidth">
                    <el-input type="textarea" v-model="form.info"></el-input>  <!--autocomplete="off"-->
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addClassroom">确 定</el-button><!--dialogFormVisible = false-->
            </div>
        </el-dialog>
        <el-row :gutter="10">
            <el-col :span="6">
                <el-select v-model="classroom.classroomTypeId" placeholder="请选择教室类型" clearable @change="handleInitClassrooms()">
                    <el-option v-for="item in type" :key="item.id" :label="item.typeName" :value="item.id">
                    </el-option>
                </el-select>
            </el-col>
            <el-col :span="6">
                <el-input v-model="classroom.name" placeholder="请输入标题" clearable @change="handleInitClassrooms()"/>
            </el-col>
            <el-col :span="6">
                <el-button type="primary" icon="el-icon-search" @click="handleInitClassrooms">搜索</el-button>
            </el-col>
        </el-row>
        <el-table :data="tableData" style="width: 100%" :default-sort="{prop: 'date', order: 'descending'}">
            <el-table-column prop="id" label="ID" width="50"></el-table-column>
            <el-table-column prop="name" label="教室名" width="100"></el-table-column>
            <el-table-column prop="capacity" label="教室容量" width="100"></el-table-column>
            <el-table-column prop="classroomType.typeName" label="教室类型" width="100"></el-table-column>
            <el-table-column prop="sort" label="排序" sortable width="100"></el-table-column>
            <el-table-column prop="teacher.name" label="添加人" width="100"></el-table-column>
            <el-table-column prop="inputTime" label="添加时间" width="160"></el-table-column>
            <el-table-column align="center" label="操作" width="200" :resizable="false">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" icon="el-icon-edit"
                               @click="openAddClassroom(scope.row.id)">编辑
                    </el-button>
                    <el-button size="mini" type="danger" icon="el-icon-delete"
                               @click="handleDelete(scope.row.id)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="prev, pager, next"
                :current-page="pageNum"
                :page-size="pageInfo.pageSize"
                :total="pageInfo.total"
                :hide-on-single-page="true"
                @current-change="handleInitClassrooms"
        >
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "ManageClassroomView",
        data() {
            return {
                classroom:{},
                tableData: [],
                type: [],
                dialogTableVisible: false,
                dialogFormVisible: false,
                form: {
                    id:'',
                    name: '',
                    classroomTypeId: 1,
                    capacity: '',
                    normal: true,
                    sort: '0',
                    info: ''
                },
                formLabelWidth: '100px',
                rules: {
                    classroomTypeId: [
                        {required: true, message: '请选择教室类型', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入教室名称', trigger: 'blur'}
                    ],
                    capacity: [
                        {required: true, message: '请输入教室容量', trigger: 'blur'}
                    ]
                },
                pageInfo:[],
                pageNum:1
            }
        },
        created() {
            this.handleInitClassrooms();
            this.getClassRomeType();
        },
        methods: {
            handleInitClassrooms(pageNum) {
                if(typeof pageNum == "number"){
                    this.pageNum = pageNum;
                }
                else{
                    this.pageNum=1;
                }
                console.log(this.classroom.classroomTypeId);
                this.$axios.post("http://localhost:8080/manage/classroom/list",this.classroom,{params:{pageNum:this.pageNum}}).then(response => {
                    this.pageInfo = response.data.data;
                    const data = response.data.data.list
                    for (var i in data) {
                        data[i]['inputTime'] = data[i]['inputTime'].replace("T", " ");
                    }
                    this.tableData = data;
                });
            },
            openAddClassroom(id) {
                this.dialogFormVisible = true;
                if(id){
                    this.$axios.get("http://localhost:8080/manage/classroom/findById?id="+id).then(response => {
                        this.form = response.data.data;
                    });
                }
            },
            getClassRomeType() {
                this.$axios.get("http://localhost:8080/manage/classroomType/list").then(response => {
                    this.type = response.data.data;
                });
            },
            addClassroom() {
                this.dialogFormVisible = false;
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$axios.post("http://localhost:8080/manage/classroom/save", this.form).then(response => {
                            if (response.data.code == 10001) {
                                this.$message({
                                    showClose: true,
                                    message: '该教室名已存在',
                                    type: 'error'
                                });
                            } else if (response.data.code == 200) {
                                this.handleInitClassrooms();
                                this.dialogFormVisible = false;
                                this.$message({
                                    type: 'success',
                                    message: '添加成功!'
                                });
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            handleDelete(id) {
                this.$confirm('此操作将永久删除该教室, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.get("http://localhost:8080/manage/classroom/delete",{params:{id: id}}).then(response => {
                        if(response.data.code == 200){
                            this.handleInitClassrooms();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                        else{
                            this.$message({
                                showClose: true,
                                message: '删除出错啦',
                                type: 'error'
                            });
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    }
</script>

<style scoped>

</style>